package com.chaodev.base.excehander;

import com.chaodev.base.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * vcbase
 *
 * @author 95634
 * @描述
 * @date 2023/5/18 11:47
 **/
/**
 * 异常拦截
 */
@ControllerAdvice
@Slf4j
public class ExceptionHandle {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        // 请求方式不正确
        if (e instanceof HttpRequestMethodNotSupportedException) {
            log.error("【请求方式不正确！】{}", e);
            return Result.error("请求方式不正确！"+((HttpRequestMethodNotSupportedException) e).getMethod());
        }

        if (e instanceof MaxUploadSizeExceededException) {
            log.error("超出最大上传限度", e);
            return Result.error("上传失败，文件过大！");
        }
        if(e instanceof MethodArgumentNotValidException){
            log.error("传递参数有误", e);
            return Result.error("参数传递有误！"+e.getMessage());
        }

        log.error("【系统异常】{}", e);
        return Result.error("系统异常！"+e.getMessage());
    }
}
